package com.quzhi.sys.interceptor;

import com.quzhi.common.CustomException;
import com.quzhi.common.GlobalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("------------进入拦截器------------");

        String token = request.getHeader("qz-Token");
        if (token == null) {
            throw new CustomException("用户未登录");
        }else{
            Object user = redisTemplate.opsForValue().get(token);
            if(user == null){
                throw new CustomException("登录信息有误，请重新登录");
            }
        }
        log.info("pass");
        return true;
    }
}
